home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / sql-bench / test-connect < prev    next >
Encoding:
Text File  |  2004-10-28  |  9.6 KB  |  332 lines

  1. #!/usr/bin/perl
  2. # Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Library General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. # Library General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Library General Public
  15. # License along with this library; if not, write to the Free
  16. # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  17. # MA 02111-1307, USA
  18. #
  19. # This test is for testing the speed of connections and sending
  20. # data to the client.
  21. #
  22. # By changing the variable '$opt_loop_count' value you can make this test
  23. # easier/harderto your computer to execute. You can also change this value
  24. # by using option --loop_value='what_ever_you_like'.
  25. ##################### Standard benchmark inits ##############################
  26.  
  27. use Cwd;
  28. use DBI;
  29. use Benchmark;
  30.  
  31. $opt_loop_count=100000;    # Change this to make test harder/easier
  32. $str_length=65000;    # This is the length of blob strings in PART:5
  33. $max_test=20;        # How many times to test if the server is busy
  34.  
  35. $pwd = cwd(); $pwd = "." if ($pwd eq '');
  36. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  37.  
  38. # This is the length of blob strings in PART:5
  39. $str_length=min($limits->{'max_text_size'},$limits->{'query_size'}-30,$str_length);
  40.  
  41. if ($opt_small_test)
  42. {
  43.   $opt_loop_count/=100;
  44. }
  45.  
  46. $opt_loop_count=min(1000, $opt_loop_count) if ($opt_tcpip);
  47. $small_loop_count=$opt_loop_count/10; # For connect tests
  48.  
  49. print "Testing the speed of connecting to the server and sending of data\n";
  50. print "Connect tests are done $small_loop_count times and other tests $opt_loop_count times\n\n";
  51.  
  52. ################################# PART:1 ###################################
  53. ####
  54. ####  Start timeing and start connect test..
  55. ####
  56.  
  57. $start_time=new Benchmark;
  58.  
  59. print "Testing connection/disconnect\n";
  60.  
  61. $loop_time=new Benchmark;
  62. $errors=0;
  63.  
  64. for ($i=0 ; $i < $small_loop_count ; $i++)
  65. {
  66.   print "$i " if (($opt_debug));
  67.   for ($j=0; $j < $max_test ; $j++)
  68.   {
  69.     if ($dbh = DBI->connect($server->{'data_source'}, $opt_user,
  70.                 $opt_password))
  71.     {
  72.       $dbh->disconnect;
  73.       last;
  74.     }
  75.     select(undef, undef, undef, 0.01*$j);
  76.     print "$errors " if (($opt_debug));
  77.     $errors++;
  78.   }
  79.   die "Got error '$DBI::errstr' after $i connects" if ($j == $max_test);
  80.   $dbh->disconnect;
  81.   undef($dbh);
  82. }
  83. $end_time=new Benchmark;
  84. print "Warning: $errors connections didn't work without a time delay\n" if ($errors);
  85. print "Time to connect ($small_loop_count): " .
  86.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  87.  
  88. ################################# PART:2 ###################################
  89. #### Now we shall do first one connect, then simple select
  90. #### (select 1..) and then close connection. This will be
  91. #### done $small_loop_count times.
  92.  
  93. if ($limits->{'select_without_from'})
  94. {
  95.   print "Test connect/simple select/disconnect\n";
  96.   $loop_time=new Benchmark;
  97.  
  98.   for ($i=0; $i < $small_loop_count; $i++)
  99.   {
  100.     $dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password) || die $DBI::errstr;
  101.     $sth = $dbh->do("select $i") or die $DBI::errstr;
  102.     $dbh->disconnect;
  103.   }
  104.   $end_time=new Benchmark;
  105.   print "Time for connect+select_simple ($small_loop_count): " .
  106.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  107. }
  108.  
  109. ################################# PART:3 ###################################
  110. ####
  111. #### Okay..Next thing we'll do is a simple select $opt_loop_count times.
  112. ####
  113.  
  114. $dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password,
  115.             { PrintError => 0}) || die $DBI::errstr;
  116.  
  117. if ($limits->{'select_without_from'})
  118. {
  119.   print "Test simple select\n";
  120.   $loop_time=new Benchmark;
  121.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  122.   {
  123.     $sth = $dbh->do("select $i") or die $DBI::errstr;
  124.   }
  125.   $end_time=new Benchmark;
  126.   print "Time for select_simple ($opt_loop_count): " .
  127.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  128. }
  129.  
  130. ###########################################################################
  131. #### The same as the previous test, but always execute the same select
  132. #### This is done to test the query cache for real simple selects.
  133.  
  134. if ($limits->{'select_without_from'})
  135. {
  136.   print "Test simple select\n";
  137.   $loop_time=new Benchmark;
  138.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  139.   {
  140.     $sth = $dbh->do("select 10000") or die $DBI::errstr;
  141.   }
  142.   $end_time=new Benchmark;
  143.   print "Time for select_simple_cache ($opt_loop_count): " .
  144.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  145. }
  146.  
  147. ##########################################################################
  148. #### First, we'll create a simple table 'bench1'
  149. #### Then we shall do $opt_loop_count selects from this table.
  150. #### Table will contain very simple data.
  151.  
  152. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'});
  153. do_many($dbh,$server->create("bench1",
  154.                  ["a int NOT NULL",
  155.                   "i int",
  156.                   "s char(10)"],
  157.                  ["primary key (a)"]));
  158. $sth = $dbh->do("insert into bench1 values(1,100,'AAA')") or die $DBI::errstr;
  159.  
  160. if ($opt_fast && defined($server->{vacuum}))
  161. {
  162.   $server->vacuum(0,\$dbh);
  163. }
  164. $dbh->disconnect;
  165.  
  166. #
  167. # First test connect/select/disconnect
  168. #
  169. print "Testing connect/select 1 row from table/disconnect\n";
  170.  
  171. $loop_time=new Benchmark;
  172. $errors=0;
  173.  
  174. for ($i=0 ; $i < $small_loop_count ; $i++)
  175. {
  176.   for ($j=0; $j < $max_test ; $j++)
  177.   {
  178.     last if ($dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password));
  179.     $errors++;
  180.   }
  181.   die $DBI::errstr if ($j == $max_test);
  182.  
  183.   $sth = $dbh->do("select a,i,s,$i from bench1") # Select * from table with 1 record
  184.     or die $DBI::errstr;
  185.   $dbh->disconnect;
  186. }
  187.  
  188. $end_time=new Benchmark;
  189. print "Warning: $errors connections didn't work without a time delay\n" if ($errors);
  190. print "Time to connect+select_1_row ($small_loop_count): " .
  191.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  192.  
  193. #
  194. # The same test, but without connect/disconnect
  195. #
  196. print "Testing select 1 row from table\n";
  197.  
  198. $dbh = $server->connect();
  199. $loop_time=new Benchmark;
  200.  
  201. for ($i=0 ; $i < $opt_loop_count ; $i++)
  202. {
  203.   $sth = $dbh->do("select a,i,s,$i from bench1") # Select * from table with 1 record
  204.     or die $DBI::errstr;
  205. }
  206.  
  207. $end_time=new Benchmark;
  208. print "Time to select_1_row ($opt_loop_count): " .
  209.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  210.  
  211. #
  212. # Same test (as with one row) but now with a cacheable query
  213. #
  214.  
  215. $loop_time=new Benchmark;
  216.  
  217. for ($i=0 ; $i < $opt_loop_count ; $i++)
  218. {
  219.   $sth = $dbh->do("select a,i,s from bench1") # Select * from table with 1 record
  220.     or die $DBI::errstr;
  221. }
  222. $end_time=new Benchmark;
  223. print "Time to select_1_row_cache ($opt_loop_count): " .
  224.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  225.  
  226. #
  227. # The same test, but with 2 rows (not cacheable).
  228. #
  229.  
  230. print "Testing select 2 rows from table\n";
  231.  
  232. $sth = $dbh->do("insert into bench1 values(2,200,'BBB')")
  233.   or die $DBI::errstr;
  234.  
  235. $loop_time=new Benchmark;
  236.  
  237. for ($i=0 ; $i < $opt_loop_count ; $i++)
  238. {
  239.   $sth = $dbh->do("select a,i,s,$i from bench1") # Select * from table with 2 record
  240.     or die $DBI::errstr;
  241. }
  242.  
  243. $end_time=new Benchmark;
  244. print "Time to select_2_rows ($opt_loop_count): " .
  245.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  246.  
  247. #
  248. # Simple test to test speed of functions.
  249. #
  250.  
  251. if ($limits->{'functions'})
  252. {
  253.   print "Test select with aritmetic (+)\n";
  254.   $loop_time=new Benchmark;
  255.  
  256.   for ($i=0; $i < $opt_loop_count; $i++)
  257.   {
  258.     $sth = $dbh->do("select a+a+a+a+a+a+a+a+a+$i from bench1") or die $DBI::errstr;
  259.   }
  260.   $end_time=new Benchmark;
  261.   print "Time for select_column+column ($opt_loop_count): " .
  262.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  263. }
  264.  
  265. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'})
  266.   or die $DBI::errstr;
  267.  
  268. if ($opt_fast && defined($server->{vacuum}))
  269. {
  270.   $server->vacuum(0,\$dbh);
  271. }
  272.  
  273. ################################# PART:5 ###################################
  274. #### We'll create one table with a single blob field,but with a
  275. #### huge record in it and then we'll do $opt_loop_count selects
  276. #### from it.
  277.  
  278. goto skip_blob_test if (!$limits->{'working_blobs'});
  279.  
  280. print "Testing retrieval of big records ($str_length bytes)\n";
  281.  
  282. do_many($dbh,$server->create("bench1", ["b blob"], []));
  283. $dbh->{LongReadLen}= $str_length; # Set retrieval buffer
  284.  
  285. my $string=(A) x ($str_length); # This will make a string $str_length long.
  286. $sth = $dbh->prepare("insert into bench1 values(?)") or die $dbh->errstr;
  287. $sth->execute($string) or die $sth->errstr;
  288. undef($string);
  289. if ($opt_fast && defined($server->{vacuum}))
  290. {
  291.   $server->vacuum(0,\$dbh);
  292. }
  293.  
  294. $loop_time=new Benchmark;
  295.  
  296. for ($i=0 ; $i < $small_loop_count ; $i++)
  297. {
  298.   $sth = $dbh->prepare("select b,$i from bench1");
  299.   if (!$sth->execute || !(@row = $sth->fetchrow_array) ||
  300.       length($row[0]) != $str_length)
  301.   {
  302.     warn "$DBI::errstr - ".length($row[0])." - $str_length **\n";
  303.   }
  304.   $sth->finish;
  305. }
  306.  
  307. $end_time=new Benchmark;
  308. print "Time to select_big_str ($small_loop_count): " .
  309.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  310.  
  311. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'})
  312.   or do
  313. {
  314.     # Fix for Access 2000
  315.     die $dbh->errstr if (!$server->abort_if_fatal_error());
  316. };
  317.  
  318. if ($opt_fast && defined($server->{vacuum}))
  319. {
  320.   $server->vacuum(0,\$dbh);
  321. }
  322.  
  323. skip_blob_test:
  324.  
  325. ################################ END ###################################
  326. ####
  327. #### End of the test...Finally print time used to execute the
  328. #### whole test.
  329.  
  330. $dbh->disconnect;
  331. end_benchmark($start_time);
  332.